/** 
*@Project: UIBase
*@File: BaseFragment.java
*@Author: linlin.zou
*@Date: 2014-12-3 
*@Copyright: 2014 www.boshtc.com Inc. All rights reserved.


*/
package com.alipear.uibase;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class BaseFragment extends Fragment implements ExternActivityInterface {
    private ExternActivity mExten;
    private Activity mParentActivity;

    private boolean mbIsChecked;

    public void setChecked(boolean flag) {
        mbIsChecked = flag;
    }

    public boolean isChecked() {
        return  mbIsChecked;
    }

    public Context getContext() {
        return getActivity().getBaseContext();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mExten = new ExternActivity(getActivity());
        mExten.onCreate(savedInstanceState);
        mParentActivity = getActivity();
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        mExten.onDestroy();
        mExten = null;
    }
    
    @Override
    public void onResume() {
        super.onResume();
        mExten.onResume();
    }

    
    @Override
    public void onPause() {
        super.onPause();
        mExten.onPause();
    }

    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    public void onDetach() {
        super.onDetach();
    }
    
    @Override
    public boolean isResume() {
        return mbIsChecked && mExten.isResume();
    }
    
    @Override
    public void showProgressDialog() {
        if(mExten == null){
            return;
        }else{
            mExten.showProgressDialog();
        }
      }

    @Override
    public void showProgressDialog(String message) {
        if(mExten == null){
            return;
        }else{
            mExten.showProgressDialog(message);
        }
    }
    
    @Override
    public void closeProgressDailog() {
        if(mExten == null){
            return;
        }else{
            mExten.closeProgressDailog();
        }
    }
    
    @Override
    public Handler getBaseHandler() {
        return mExten.getBaseHandler();
    }
    
    @Override
    public Activity getCurActivity() {
        return mParentActivity;
    }
    
    public View findViewById(int id) {
        return mRootView.findViewById(id);
    }

    View mRootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (mRootView != null) {
            ViewGroup tmp = (ViewGroup)mRootView.getParent();
            if (tmp != null) {
                tmp.removeView(mRootView);
            }
            return mRootView;
        }
        mRootView = inflater.inflate(getLayoutId(), container, false);
        initView();
        return mRootView;
    }

    public View getRootView() {
        return mRootView;
    }

    public abstract void initView();

    public abstract int getLayoutId();

}
